<template>
	<view  :class="[showDel?'':'hide-delete-btn']">
		<u-upload	    
		     :fileList="fileList1" 
			 @afterRead="afterRead" 
			 @delete="deletePic" 
			 name="1" multiple 
			 :maxCount="limit"
			  width="111" 
			  height="105"
			  :deletable="showDel">
			<view class="flie-inner">
				<image class="flie-inner-img" src="@/static/common/icon-upload.png"></image>
				<view>{{tipText}}</view>
			</view>
		</u-upload>
	</view>
	
</template>


<script>
	import {
		getToken
	} from "@/common/auth.js"
	export default {
		props: {
			fileList1: [String, Object, Array],
			// 图片数量限制
			limit: {
				type: Number,
				default: 1
			},
			showDel:{
				default: true
			},
			tipText:{
				default:'点击上传照片'
			}

		},

		onLoad(options) {},

		data() {
			return {
				// fileList1: [],
			}
		},

		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.$emit('getImg', this[`fileList${event.name}`])
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {

					// this.$emit('getImg',{
					// 	...item,
					// 	status: 'uploading',
					// 	message: '上传中'
					// })
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
					this.$emit('getImg', this[`fileList${event.name}`])
				}
			},
			uploadFilePromise(url) {
				let that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: that.baseUrl + '/api/upload',
						filePath: url,
						name: 'file',
						header: {
							'Authorization': 'Bearer ' + getToken()
						},
						formData: {
							user: 'test'
						},
						success: (res) => {
							let data = JSON.parse(res.data)
							setTimeout(() => {
								resolve(data.fileName)
							}, 1000)
						}
					});
				})
			},




		},
	}
</script>


<style lang="scss" scoped>
	
	.flie-inner {
	
		width: 200rpx;
		height: 200rpx;
		background: #F4F4F5;
		border-radius: 16rpx;
		border: 1rpx solid #DDDDDD;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 24rpx;
		color: #11AA66;

	}

	.flie-inner-img {
		width: 44upx;
		height: 42upx;
		margin-bottom: 30upx;
	}

	 .hide-delete-btn{
		::v-deep .u-upload__deletable{
			 display: none !important; /* 隐藏删除按钮 */
		}
	} 
	
	/* #ifdef  MP-WEIXIN */
	/*  只在小程序中生效  */
	::v-deep .hide-delete-btn{
		::v-deep .u-upload__deletable{
			 display: none !important; /* 隐藏删除按钮 */
		}
		.u-upload__deletable{
			 display: none !important; /* 隐藏删除按钮 */
		}
	} 
	/*  #endif  */
</style>